package com.xjwlmq.exam20241012;

import com.alibaba.fastjson.JSON;
import com.xjwlmq.exam20241011.DictType;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName WorkerUpdateServlet
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/12 20:54
 * @Version 1.0
 **/
@WebServlet("/updateWorker")
public class WorkerUpdateServlet extends HttpServlet {
    WorkerDaoImpl workerDao = new WorkerDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html:charset=UTF-8");

        Worker thisWorker = new Worker();
        thisWorker.setWorkerId(Integer.parseInt(req.getParameter("workerId")));
        thisWorker.setDepartmentName(req.getParameter("departmentName"));
        thisWorker.setWorkerDate(req.getParameter("workerDate"));
        thisWorker.setWages(Double.parseDouble(req.getParameter("wages")));
        thisWorker.setPolitics(req.getParameter("politics"));
        thisWorker.setName(req.getParameter("name"));
        thisWorker.setBirthDate(req.getParameter("birthDate"));

        int count = workerDao.updateWorkerViaDictId(thisWorker);
        if (count > 0) {
            resp.getWriter().write(JSON.toJSONString("成功修改了%d行数据.".formatted(count)));
        } else {
            resp.getWriter().write(JSON.toJSONString("没有影响数据..."));
        }
    }
}
